package cc.ufinity.networkdiagnose.manager;

import java.io.InputStream;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import cc.ufinity.networkdiagnose.util.StringUtils;

/**
 * 检测手机网络类
 * 
 * @author ZR
 * 
 */
public final class NetworkManager extends AbstractManager {
    public static final int TYPE_MOBILE = 1;
    public static final int TYPE_WIFI = 2;
    private String pingResult = "";

    public String getPingResult() {
        return pingResult;
    }

    public static int detectNetworkConnectivity(Context context) {
        boolean isconnect = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null == cm)
            return -1;
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (null == ni)
            return -1;
        isconnect = ni.isConnected();
        // switch (networkType) {
        // case TYPE_MOBILE:
        // isconnect = ni.isConnected() && ConnectivityManager.TYPE_MOBILE ==
        // ni.getType();
        // break;
        // case TYPE_WIFI:
        // isconnect = ni.isConnected() && ConnectivityManager.TYPE_WIFI ==
        // ni.getType();
        // break;
        // default:
        // isconnect = ni.isConnected() && (ConnectivityManager.TYPE_MOBILE ==
        // ni.getType() || ConnectivityManager.TYPE_WIFI == ni.getType());
        // }
        //if (isconnect) {
          //  NetworkManager nm = new NetworkManager();
           // isconnect = nm.ping("www.baidu.com", 1, 32);
        //}
        if (isconnect)
            return ni.getType();
        else
            return -1;

    }

    public boolean ping(final String address, final int frequency, final int bytes) {
        Runtime run = Runtime.getRuntime();
        Process proc = null;
        String str = "ping -c " + frequency + " -s " + bytes + " " + address;
        try {
        	System.out.println("+++ PING 开始 +++");
            proc = run.exec(str);
            int state = proc.waitFor();
            if (state == 0) {
                InputStream is = proc.getInputStream();
                try {
                    pingResult = StringUtils.convertStream(is);
                } catch(Exception e) {}
                is.close();
                sendMsg(MessageWhat.PING_SUCCESS);
                System.out.println("+++ PING 成功 +++");
                return true;
            } else {
                sendMsg(MessageWhat.PING_FAILED);
                return false;
            }
        } catch (Exception e) {
            Log.e("Ping", "Ping测试出错：", e);
            sendMsg(MessageWhat.PING_FAILED);
            return false;
        }
    }
  /*  public void tracert() {
//    	 String cmd = String.format("echo %s > %s\n", arg, mSwitchUsbFileTextEdit.getText().toString());     
    	 try {           
    		 Process exeEcho = Runtime.getRuntime().exec("su");      
//    		 exeEcho.getOutputStream().write(cmd.getBytes());       
//    		 exeEcho.getOutputStream().flush();      
    	 } catch (IOException e) {   
//    			 showMessage("Excute exception: " + e.getMessage());     
    	 }
	}*/ 
    public boolean pingForRoam(final String address, final int frequency, final int bytes) {
        Runtime run = Runtime.getRuntime();
        Process proc = null;
        String str = "ping -c " + frequency + " -s " + bytes + " " + address;
        try {
            proc = run.exec(str);
            int state = proc.waitFor();
            if (state == 0) {
                InputStream is = proc.getInputStream();
                pingResult = StringUtils.convertStream(is);
                is.close();
                sendMsg(MessageWhat.ROAM_SUCCESS);
                return true;
            } else {
                sendMsg(MessageWhat.ROAM_FAILED);
                return false;
            }
        } catch (Exception e) {
            Log.e("Ping", "Ping测试出错：", e);
            sendMsg(MessageWhat.PING_FAILED);
            return false;
        }
    }
}
